package com.dykj.fireFighting.web.controller;

import com.dykj.fireFighting.api.socialUnit.devicemanger.dto.ManufactorDto;
import com.dykj.fireFighting.api.socialUnit.devicemanger.entity.Manufactor;
import com.dykj.fireFighting.api.socialUnit.devicemanger.service.IManufactorService;
import com.dykj.fireFighting.common.core.annotation.AutoLog;
import com.dykj.fireFighting.common.core.api.CommonResult;
import com.dykj.fireFighting.common.core.dto.CommonDto;
import com.dykj.fireFighting.common.core.exception.BaseException;
import com.dykj.fireFighting.common.core.vo.PageVo;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.base.shiro.ShiroUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 主机管理接口
 *
 * @Author aixiaolan
 * @Date  2021-06-15 15:50:59
 */
@Slf4j
@Api(value = "主机管理接口", tags = {"主机管理接口"})
@RestController
@RequestMapping("manufactor")
public class ManufactorController {

    @DubboReference
    private IManufactorService manufactorService;

    @Autowired
    private RedisService redisService;

    /**
     * 获取单位id
     * @return String
     */
    public String getEnterpriseId() {
        try {
            return Long.toString(ShiroUtil.getUser().getEnterpriseId());
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 获取用户名称
     * @return String
     */
    public String getName() {
        try {
            return ShiroUtil.getUser().getName();
        } catch (NullPointerException e) {
            e.printStackTrace();
            throw new BaseException("请登录系统");
        }
    }

    /**
     * 主机管理列表
     *
     * @return
     */
    @AutoLog(value = "主机管理-分页列表查询")
    @ApiOperation(value = "主机管理分页列表", notes = "主机管理分页列表")
    @GetMapping("/list")
//    @RequiresPermissions("unit/manufactor/list")
    public CommonResult<PageVo<Manufactor>> treeGrid(ManufactorDto commonDto) {
        commonDto.setUnitId(getEnterpriseId());
        PageVo<Manufactor> page = manufactorService.pageList(commonDto);
        return CommonResult.success(page);
    }

    /**
     * 获取主机管理信息
     */
    @AutoLog(value = "主机管理-获取主机管理信息")
    @ApiOperation(value = "获取主机管理信息", notes = "获取主机管理信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", dataType = "String", required = true)
    @GetMapping("/info/{id}")
//    @RequiresPermissions("unit/manufactor/info")
    public CommonResult<Manufactor> info(@PathVariable("id") String id){
        Manufactor manufactor = manufactorService.getById(id);
        return CommonResult.success(manufactor);
    }

    /**
     * 保存主机管理信息
     */
    @AutoLog(value = "主机管理-保存主机管理信息")
    @ApiOperation(value = "保存主机管理信息", notes = "保存主机管理信息")
    @PostMapping("/save")
//    @RequiresPermissions("unit/manufactor/save")
    public CommonResult save(@Valid @RequestBody Manufactor manufactor) {
        try {
            log.info("主机管理新增：{}",manufactor.toString());
            manufactor.setCreateTime(new Date());
            manufactor.setUnitId(getEnterpriseId());
            manufactor.setCreateBy(getName());
            // manufactor.setUpdateTime(new Date());
            manufactorService.save(manufactor);
            return CommonResult.success("添加成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 修改主机管理信息
     */
    @AutoLog(value = "主机管理-修改主机管理信息")
    @ApiOperation(value = "修改主机管理信息", notes = "修改主机管理信息")
    @PostMapping("/update")
//    @RequiresPermissions("unit/manufactor/update")
    public CommonResult update(@Valid @RequestBody Manufactor manufactor) {
        try {
            // manufactor.setUpdateTime(new Date());
            manufactorService.updateById(manufactor);
            //更新主机时删除redis对应的key
            redisService.del(manufactor.getIdentification());
            return CommonResult.success("修改成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }

    /**
     * 删除主机管理信息
     */
    @AutoLog(value = "主机管理-删除主机管理信息")
    @ApiOperation(value = "删除主机管理信息", notes = "删除主机管理信息")
    @ApiImplicitParam(paramType = "query", name = "ids", value = "ID数组", dataType = "String", required = true, allowMultiple = true)
    @PostMapping("/delete")
//    @RequiresPermissions("unit/manufactor/delete")
    public CommonResult delete(@RequestBody String[]ids) {
        try {
            manufactorService.removeByIds(Arrays.asList(ids));
            return CommonResult.success("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BaseException("运行异常，请联系管理员");
        }
    }
    /**
     * 根据系统类型获取主机
     *
     * @return
     */
    @ApiOperation(value = "根据系统类型获取主机", notes = "根据系统类型获取主机")
    @GetMapping("/queryByDeviceSysType")
    public CommonResult queryByDeviceSysType(String deviceSysType){
        Long enterpriseId = ShiroUtil.getUser().getEnterpriseId();
        List<Manufactor> manufactors = manufactorService.queryBySysType(enterpriseId + "", deviceSysType);
        return CommonResult.success(manufactors);
    }
}
